package com.atguigu.tingshu.order.pattern.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.order.pattern.TradeStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName : VIPTrade
 * Package : com.atguigu.tingshu.order.pattern.impl
 * Description :
 * <p>
 * Author: LHZ
 * Create: 2025/8/4 15:16
 * Version: 1.0
 */
@Slf4j
@SuppressWarnings("all")
@Component(SystemConstant.ORDER_ITEM_TYPE_VIP)
public class VIPTrade implements TradeStrategy {

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public OrderInfoVo trade(Long userId, TradeVo tradeVo) {
        //创建返回结果对象
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        //创建订单明细列表
        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        //创建订单减免明细列表
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        //声明金额相关变量
        BigDecimal originPrice = new BigDecimal("0.00");
        BigDecimal discountPrice = new BigDecimal("0.00");
        BigDecimal derateAmount = new BigDecimal("0.00");

        //如果付款项目为vip会员
        //调用远程服务根据付款项目id获取到具体的vip套餐
        VipServiceConfig vipServiceConfig = userFeignClient.getVipServiceConfig(tradeVo.getItemId()).getData();
        Assert.notNull(vipServiceConfig,"vip会员套餐id:{}为null",tradeVo.getItemId());

        //根据原始金额-实际金额计算减免金额
        originPrice = vipServiceConfig.getPrice(); //原始价格
        discountPrice = vipServiceConfig.getDiscountPrice(); //实际价格
        if(originPrice.compareTo(discountPrice) == 1){
            derateAmount = originPrice.subtract(discountPrice);
        }

        //声明订单明细对象,添加到订单明细列表
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(vipServiceConfig.getId());
        orderDetailVo.setItemName("会员套餐：" + vipServiceConfig.getName());
        orderDetailVo.setItemPrice(originPrice);
        orderDetailVo.setItemUrl(vipServiceConfig.getImageUrl());
        orderDetailVoList.add(orderDetailVo);

        //如果有优惠,封装订单减免vo对象,加入到订单减免列表
        if(originPrice.compareTo(discountPrice) == 1){
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT); //订单减免类型 1405-专辑折扣 1406-VIP服务折
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("套餐限时减免：" + derateAmount);
            orderDerateVoList.add(orderDerateVo);
        }

        orderInfoVo.setItemType(tradeVo.getItemType()); //付款项目类型: 1001-专辑 1002-声音 1003-vip会员
        orderInfoVo.setOriginalAmount(originPrice);  //原价
        orderInfoVo.setDerateAmount(derateAmount);   //优惠价
        orderInfoVo.setOrderAmount(discountPrice);   //实际价
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        return orderInfoVo;
    }
}